﻿namespace Baggr.FeedParser.Rss
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Xml.Serialization;

    /// <summary>
    /// Элемент RSS ленты.
    /// </summary>
    [Serializable]
    [DebuggerStepThrough]
    [DesignerCategory("code")]
    public class RssItem
        : IFeedItem
    {
        [XmlElement("author")]
        public string Author { get; set; }

        /// <summary>
        /// Дата публикации.
        /// </summary>
        DateTime IFeedItem.PublicationDate
        {
            get
            {
                if (PubDate == null)
                    return new DateTime();

                return DateTime.Parse(PubDate);
            }
        }

        /// <summary>
        /// Локальный идентификатор записи.
        /// </summary>
        Guid IFeedItem.LocalId
        {
            get
            {
                if (Guid == null || string.IsNullOrEmpty(Guid.Value))
                {
                    return (Title ?? string.Empty + Description ?? string.Empty).GetGuidByString();
                }

                return Guid.Value.GetGuidByString();
            }
        }

        string IFeedItem.Title
        {
            get
            {
                return Title ?? string.Empty;
            }
        }

        string IFeedItem.Description
        {
            get
            {
                return this.Description ?? string.Empty;
            }
        }

        [XmlElement("category")]
        public RssCategory Category { get; set; }

        [XmlElement("comments", DataType = "anyURI")]
        public string Comments { get; set; }

        /// <summary>
        /// Ссылка на запись.
        /// </summary>
        Uri IFeedItem.Link
        {
            get
            {
                if (string.IsNullOrEmpty(Link))
                    return null;

                return new Uri(Link);
            }
        }

        [XmlElement("description")]
        public string Description { get; set; }

        [XmlElement("enclosure")]
        public RssEnclosure Enclosure { get; set; }

        [XmlElement("guid")]
        public RssGuid Guid { get; set; }

        [XmlElement("link", DataType = "anyURI")]
        public string Link { get; set; }

        [XmlElement("pubDate")]
        public string PubDate { get; set; }

        [XmlElement("source")]
        public RssSource Source { get; set; }

        [XmlElement("title")]
        public string Title { get; set; }
    }
}
